// NationscapeProtests
log using NationscapeProtests.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\Nationscapedataset.dta" // Tausanovitch, Chris and Lynn Vavreck. 2020. Democracy Fund + UCLA Nationscape, October 10-17, 2019 (version 20200814). Retrieved from [URL].  Date accessed: March 09, 2025.

*Adjust date format
gen dateofinterview = dofc(start_date) 
format dateofinterview %td

gen cutoff_date = date("25may2020", "DMY")  
drop if dateofinterview < cutoff_date

keep reparations group_favorability_blm statements_gender_identity group_favorability_undocumented dateofinterview response_id congress_district age race_ethnicity household_income education right_track pid3 sen_knowledge interest pres_approval gender 

// Delete missing variables and recode
local vars group_favorability_blm reparations right_track pres_approval statements_gender_identity group_favorability_undocumented

foreach var in `vars' {
    replace `var' = . if `var' > 20
}

replace right_track=. if right_track>2
replace sen_knowledge=2 if sen_knowledge==999 // Don't knows in senate knowledge question now coded as incorrect
replace pid3=. if pid3>3
replace pres_approval=. if pres_approval>10

joinby congress_district using "C:\Users\sbstjp\OneDrive - Cardiff University\blmprotestswithatleasthundredcondist.dta" 

// Retain only protests/interviews within 90 day post protest period
*Convert dateofinterview to Stata date format
format dateofinterview %td
gen interview_date = dateofinterview 
format interview_date %td // Ensure displayed as a readable date

*Convert event_date to Stata date format
gen protest_date = date(substr(event_date, 1, 10), "YMD") // Extract the date part and convert
format protest_date %td // Format as readable date

*Calculate the difference in days
gen days_difference = interview_date - protest_date

*Retain rows where the protest occurred within 90 days before the interview
keep if days_difference >= 0 & days_difference <= 90

*Data now has observations with at least one protest 
collapse (mean) reparations group_favorability_blm statements_gender_identity group_favorability_undocumented dateofinterview age race_ethnicity household_income education right_track pid3 sen_knowledge interest pres_approval gender, by(response_id) 

// Merge with a dataset of respondents who didn't experience protests, prior to matching
merge 1:1 response_id using "C:\Users\sbstjp\OneDrive - Cardiff University\dataset_with_and_without_protests.dta" 

// Keep Dems only
keep if pid3==1 

// The treatment is whether respondents lived in a congressional district with a protest
gen treatment =. 
replace treatment = 1 if _merge==3 // In obs with merge=3, there were protests
replace treatment = 0 if _merge==2 // In those with merge=2, there were no protests

*Rename so consistent with Stata character limits after next step
rename group_favorability_blm blm
rename group_favorability_undocumented undocumented
rename statements_gender_identity genderidentity

*Reverse variables so social justice is coded high
foreach var in blm reparations undocumented {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' - `var'
}

logit treatment right_track household_income education age sen_knowledge interest pres_approval, vce(robust)
predict p_score, pr

// Run exploratory models
psmatch2 treatment, outcome(rblm) p(p_score) neighbor(1) 
psmatch2 treatment, outcome(rreparations) p(p_score) neighbor(1) 
psmatch2 treatment, outcome(rundocumented) p(p_score) neighbor(1) 
psmatch2 treatment, outcome(genderidentity) p(p_score) neighbor(1) 

log clear